/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.converters;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.star.framework.excel.convert.CellValueConverter;
import cn.star.framework.excel.convert.ColumnAutoMount;
import cn.star.framework.excel.convert.ValueWrapper;
import cn.star.framework.excel.core.Column;
import cn.star.framework.excel.core.RowWrapper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 空字符串Java 8中基本数据类型转换<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public class StringNull8BasicTypeConverter
    implements CellValueConverter<Object, String>, ColumnAutoMount {
  public static final StringNull8BasicTypeConverter INSTANCE = new StringNull8BasicTypeConverter();

  private static final List<Class<?>> NUMBER_TYPES =
      CollectionUtil.toList(
          char.class, byte.class, short.class, int.class, long.class, float.class, double.class);
  private static final List<Class<?>> BOOLEAN_TYPES = Collections.singletonList(boolean.class);

  @Override
  public Object convert(Column column, String v, RowWrapper wrapper) {
    if (StrUtil.isBlank(v)) {
      Class<?> type = column.getType();

      if (type != null) {
        if (NUMBER_TYPES.contains(type)) {
          return 0;
        } else if (BOOLEAN_TYPES.contains(type)) {
          return false;
        }
      }
    }

    return v;
  }

  @Override
  public String convert(Column column, ValueWrapper<Object> wrapper) {
    return wrapper.getValue() == null ? "" : wrapper.getValue().toString();
  }

  @Override
  @SuppressWarnings("rawtypes")
  public Class<?>[] getSupportTypes() {
    List<Class> classes = new ArrayList<>();
    classes.addAll(NUMBER_TYPES);
    classes.addAll(BOOLEAN_TYPES);

    return ArrayUtil.toArray(classes, Class.class);
  }
}
